from log import WriteLog


class CompareWrite:
    def __init__(self):
        self.log = WriteLog()

    @staticmethod
    def compare(list1, list2, v1, v2, desc, up_user, di_user):
        text = ''
        same_list = list()
        diff_dict = {'cmd': desc, 'diff': []}
        list1_to_str = '\n'.join(list1).replace(' ', '').replace(
            up_user, '').replace(di_user, '')
        list2_to_str = '\n'.join(list2).replace(' ', '').replace(
            up_user, '').replace(di_user, '')
        if len(list1) < len(list2):
            max_num = len(list2)
        else:
            max_num = len(list1)
        for i in range(max_num):
            if i < len(list1) and i < len(list2):
                if list1[i].replace(' ', '').replace(up_user, '').replace(
                        di_user, '') not in list2_to_str:
                    diff_dict['diff'].append(
                        {'upgrade': list1[i], 'direct': ''})
                    text += f'upgrade{v1}:\033[32m{list1[i]}\033[0m' \
                        f' ---VS--- ' \
                        f'direct{v2}:\033[32m""\033[0m\n'
                else:
                    same_list.append(list1[i])
                if list2[i].replace(' ', '').replace(up_user, '').replace(
                        di_user, '') not in list1_to_str:
                    diff_dict['diff'].append(
                        {'upgrade': '', 'direct': list2[i]})
                    text += f'upgrade{v1}:\033[32m""\033[0m' \
                        f' ---VS--- ' \
                        f'direct{v2}:\033[32m{list2[i]}\033[0m\n'
                else:
                    if list2[i] not in same_list:
                        same_list.append(list2[i])
            else:
                if i < len(list1):
                    if list1[i].replace(' ', '').replace(up_user, '').replace(
                            di_user, '') not in list2_to_str:
                        diff_dict['diff'].append(
                            {'upgrade': list1[i], 'direct': ''})
                        text += f'upgrade{v1}:\033[32m{list1[i]}\033[0m' \
                            f' ---VS--- ' \
                            f'direct{v2}:\033[32m""\033[0m\n'
                elif i < len(list2):
                    if list2[i].replace(' ', '').replace(up_user, '').replace(
                            di_user, '') not in list1_to_str:
                        diff_dict['diff'].append(
                            {'upgrade': '', 'direct': list2[i]})
                        text += f'upgrade{v1}:\033[32m""\033[0m' \
                            f' ---VS--- ' \
                            f'direct{v2}:\033[32m{list2[i]}\033[0m\n'
        text += '\n\n\n'
        return same_list, diff_dict
